package sealte.hvpms.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import sealte.hvpms.dao.AdminDAO;
import sealte.hvpms.pojo.Admin;
import sealte.hvpms.util.DBConnection;

public class AdminDAOImpl implements AdminDAO{
	private Connection conn = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
		
	public String login(String name,String password){
		String message = "用户名或密码不正确";
		String sql = "select admin_id from hvpms_admin where admin_name=? and admin_password=md5(?)";
		try {
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(rs.next()){
				int res = rs.getInt(1);
				if(res>-1){
					message = "OK";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBConnection.close(rs, ps, conn);
		}
		
		return message;
	}
	
	public Admin getAdminAll(String name,String password){
		Admin admin = null;
		String sql = "select admin_id,admin_level from hvpms_admin where admin_name=? and admin_password=md5(?)";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(rs.next()){
				admin = new Admin();
				admin.setId(rs.getInt("admin_id"));
				admin.setName(name);
				admin.setPassword(password);
				admin.setLevel(rs.getString("admin_level"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs, ps, conn);
		}
		return admin;
	}
	
	public boolean addNew(String name,String password){
		boolean flag = false;
		String sql = "insert into hvpms_admin(admin_name,admin_password) values(?,md5(?))";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, password);
			ps.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
	
	public List<Admin> getAll(){
		List<Admin> all = new ArrayList<Admin>();
		String sql = "select * from hvpms_admin";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				Admin admin = new Admin();
				admin.setId(rs.getInt("admin_id"));
				admin.setName(rs.getString("admin_name"));
				admin.setLevel(rs.getString("admin_level"));
				all.add(admin);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs, ps, conn);
		}
		return all;
	}
	
	public boolean modifyPassword(int id,String password){
		boolean flag = false;
		String sql = "update hvpms_admin set admin_password=md5(?) where admin_id=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, password);
			ps.setInt(2, id);
			ps.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
	
	public boolean modifyLevel(int id,String level){
		boolean flag = false;
		String sql = "update hvpms_admin set admin_level=? where admin_id=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, level);
			ps.setInt(2, id);
			ps.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
	
	public String getNameByID(int id){
		String name = "";
		String sql = "select admin_name from hvpms_admin where admin_id=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if(rs.next()){
				name = rs.getString("admin_name");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs, ps, conn);
		}
		return name;
	}
	
	public boolean deleteByID(int id){
		boolean flag = false;
		String sql = "delete from hvpms_admin where admin_id=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(null, ps, conn);
		}
		return flag;
	}
	
	public boolean validatePassword(int id,String password){
		boolean flag = false;
		String sql = "select * from hvpms_admin where admin_id=? and admin_password=md5(?)";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(rs.next()){
				flag = true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs,ps, conn);
		}
		return flag;
	}
	
	public boolean isNameExist(String name){
		boolean flag = false;
		String sql = "select * from hvpms_admin where admin_name=?";
		try{
			conn = DBConnection.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			if(rs.next()){
				flag = true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.close(rs,ps, conn);
		}
		return flag;
	}
}
